//
//  YJStatusCell.swift
//  YJWeiBo
//
//  Created by Jim1024 on 2017/1/17.
//  Copyright © 2017年 Jim1024. All rights reserved.
//

import UIKit
import SnapKit

class YJStatusCell: UITableViewCell {
    
    /// bottomView的顶部约束
    var bottomViewTopCons: Constraint?

    var status: YJStatus? {
        didSet {
            originalView.status = status
            
            // 判断是否有被转的微博模型,有被转发的微博模型就显示被转发微博的view如果没有被转发的微博模型,就不显示被转发微博的view
            bottomViewTopCons?.deactivate()
            if let retweetStatus = status?.retweeted_status {
                retweetView.isHidden = false
                retweetView.retweetStatus = retweetStatus
                bottomView.snp.makeConstraints({ (make) in
                    self.bottomViewTopCons = make.top.equalTo(retweetView.snp.bottom).constraint
                })
            } else {
                retweetView.isHidden = true
                bottomView.snp.makeConstraints({ (make) in
                    self.bottomViewTopCons = make.top.equalTo(originalView.snp.bottom).constraint
                })
            }
            self.setNeedsLayout()
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        inintCustomView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func inintCustomView() {
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomView)
        bottomView.alpha = 0.7
        
        originalView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(10)
            make.leading.trailing.equalToSuperview()
        }
        
        retweetView.snp.makeConstraints { (make) in
            make.top.equalTo(originalView.snp.bottom)
            make.leading.trailing.equalToSuperview()
        }
        
        bottomView.snp.makeConstraints { (make) in
            self.bottomViewTopCons = make.top.equalTo(retweetView.snp.bottom).constraint
            make.leading.trailing.equalToSuperview()
            make.height.equalTo(44)
        }
        
        // 3.当使用autolayout来约束cell里面的控件,并且cell的高度想通过autolayout来自动确定,需要给contentView添加约束
        contentView.snp.makeConstraints { (make) in
            make.top.leading.trailing.equalToSuperview()
            make.bottom.equalTo(bottomView)
        }
        
        // cell的优化
        // 异步绘制
//        self.layer.drawsAsynchronously = true
//        // 栅格化/光栅化会离屏渲染
//        self.layer.shouldRasterize = true
//        self.layer.rasterizationScale = UIScreen.main.scale
    }
    
    // MARK: - 懒加载
    /// 原创微博View
    fileprivate lazy var originalView: YJStatusOriginalView = YJStatusOriginalView()
    
    /// 底部视图
    fileprivate lazy var bottomView: YJStatusBottomView = YJStatusBottomView()
    
    /// 被转发微博的View
    fileprivate lazy var retweetView: YJStatusRetweetView = YJStatusRetweetView()

}
